package study.rabbitmq.consumer.limit;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import pojo.User;
import study.rabbitmq.consumer.config.RabbitConfig;

import java.io.IOException;

/**
 * 消费端限流，限制接收的消息
 * 指定了containerFactory 限制了消费端没有返回应答时只接收两个
 */
@Component
@RabbitListener(queues = RabbitConfig.LIMIT_QUEUE,containerFactory = "containerFactory")
public class LimitConsumerMsg {

    @RabbitHandler
    public void limitMsg(User user, Channel channel, Message message) {
        //int i = 1/0;
        System.out.println(user.getName()+":"+user.getAge());
        try {
            // 手动确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
